#!/usr/bin/env python3

import sys, json
from common import fget, fset

categories = [
    "major",
    "minor",
    "release",
    "build",
    "test",
]
default_category = "build"

usage_msg_fmt = \
"""usage: {{}} [CATEGORY]
  CATEGORY is one of: {}
    default: {}""".format(" ".join(categories), default_category)
def main (argv0, argv):
    if argv:
        try:
            index = categories.index(argv[0])
        except ValueError:
            return usage_msg_fmt.format(argv0)
    else:
        index = categories.index(default_category)
    
    version = list(map(int, fget("vstring").split(".")))
    if index >= len(version):
        version += [0]*(index - len(version) + 1)
    else:
        del version[index+1:]
    version[index] += 1
    if index == 0:
        version += [0]
    fset("vstring", ".".join(map(str, version)))

if __name__ == "__main__":
    try:
        sys.exit(main(sys.argv[0], sys.argv[1:]))
    except KeyboardInterrupt:
        pass
